package com.ck.common.model;

import com.ck.core.constant.ClientConstant;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

/**
 * <p>Title: OperationResult</p>
 * <p>Description: used for returning this type of object to client in ajax request circumstance</p>
 *
 * @author huangf
 * @date 2018年4月19日
 */
public class OperationResult<T> implements Serializable {

	private static final long serialVersionUID = 3779231221394313342L;

	private int code;

	private String msg;

	private T data;

	public OperationResult() {
	}

	public OperationResult(String msg, T data) {
		this.msg = msg;
		this.data = data;
	}

	public OperationResult(int code, String msg, T data) {
		this(msg, data);
		this.code = code;
	}

	public OperationResult(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public static OperationResult<Object> newSystemErrorOperationResult() {
		return new OperationResult<Object>(ClientConstant.CODE_SYSTEM_ERROR, ClientConstant.MSG_SYSTEM_ERROR);
	}

	public static OperationResult<Object> newSuccessOperationResult() {
		return new OperationResult<Object>(ClientConstant.CODE_SUCCESS, ClientConstant.MSG_SUCCESS);
	}

	public static OperationResult<Object> newSuccessOperationResult(Object data) {
		return new OperationResult<Object>(ClientConstant.CODE_SUCCESS, ClientConstant.MSG_SUCCESS, data);
	}

	public static OperationResult<Object> newSuccessOperationResult(String msg, Object data) {
		return new OperationResult<Object>(ClientConstant.CODE_SUCCESS, msg, data);
	}

	public static OperationResult<Object> newSuccessTipsOperationResult(String msg) {
		return new OperationResult<Object>(ClientConstant.CODE_SUCCESS, msg);
	}

	public static OperationResult<Object> newValidateFailOperationResult(String msg) {
		return new OperationResult<Object>(ClientConstant.CODE_PARAM_VALIDATE_FAIL, msg);
	}

	public static OperationResult<Object> newFailOperationResult(String msg) {
		return new OperationResult<Object>(ClientConstant.CODE_SYSTEM_ERROR, msg);
	}

	@JsonIgnore
	public boolean isSucess() {
		return ClientConstant.CODE_SUCCESS == this.getCode();
	}

}
